package cn.com.taiji.oospub.wechat.common.wxpay.common;

import java.lang.reflect.Field;
import java.util.*;

public class Test {
	 /**
	  * @param 
	  */
	 public static void main(String[] args) {
	  Test t = new Test();
	  //list entity 返回
	  List<TestInfo> list = t.getEntityList();
	  for(TestInfo entity :list){
		   System.out.println("A1 : " + entity.getA1());
		   System.out.println("B1 : " + entity.getB1());
		   System.out.println("C1 : " + entity.getC1());
		   System.out.println("D1 : " + entity.getD1());
		   System.out.println("E1 : " + entity.getE1());
		   System.out.println("F1 : " + entity.getF1());
		   System.out.println("G1 : " + entity.getG1());
	  }
	  //list map 返回
	  List<Map<String,Object>> listMap = t.getNewMapList();
		  for(Map<String,Object> map :listMap){
			   Set<String> keys = map.keySet();
			   for(String key:keys){
				   System.out.println(key +" : "+map.get(key));
			   }
		  }
	 }
	 
	 /**
	  * 封装成 list entity 返回
	  * @return
	  */
	 public List<TestInfo> getEntityList(){
		  List<Map<String,Object>> listMap = this.getNewMapList();
		  List<TestInfo> list =null;
		  try {
			   if(listMap!=null&&listMap.size()>0){
				    TestInfo entity = null;
				    list = new ArrayList<TestInfo>();
				    for(Map<String,Object> map:listMap){
					     entity = new TestInfo();
					     Set<String> keys = map.keySet();
					     for(String key:keys){
					    	 //System.out.println(key+": "+map.get(key));
						      String fieldName = key ;
						      Object fieldValue = map.get(key);
						      this.setValue(entity, fieldName, fieldValue);//反射赋值
					     }
					     list.add(entity);
				    }
			   }
		  } catch (Exception e) {
			  e.printStackTrace();
		  }
		  return list;
	 }
	 
	 
	 /**
	  * 反射给实体类赋值
	  * @param entity 当前实体类对象
	  * @param fieldName 实体类 字段名称
	  * @param fieldValue 实体类 字段对应的值
	  * @throws Exception
	  */
	 private void setValue(Object entity,String fieldName,Object fieldValue) throws Exception{
	  Field[] field = entity.getClass().getDeclaredFields();// 获取实体类的所有属性，返回Field数组
	  for (int j = 0; j < field.length; j++) {
	   String name = field[j].getName(); // 获取属性的名字
	   field[j].setAccessible(true);
	   if (fieldName.equals(name)) {
	    field[j].set(entity, fieldValue);// 赋值
	    break;
	   }
	  }   
	 }
	 
	 
	 /**
	  * new list  替换map的key的方法
	  * @return
	  */
	 public List<Map<String,Object>> getNewMapList(){
	  List<Map<String,Object>> newList = null;
	  List<Map<String,Object>> oldList = this.getDataMapList();
	  if(oldList!=null&&oldList.size()>0){
	   newList = new ArrayList<Map<String,Object>>();
	   Map<String,Object> keyMap = this.getKeyMap();
	   Map<String,Object> valueMap = null;
	   for(Map<String,Object> map:oldList){
	    Set<String> keys = map.keySet();
	    valueMap = new HashMap<String, Object>();
	    for(String key :keys){
	     //get value
	     Object value = map.get(key);
	     //new key
	     String newKey = keyMap.get(key).toString();
	     valueMap.put(newKey, value);
	    }
	    newList.add(valueMap);
	   }
	  }
	  return newList;
	 }

	 /**
	  * data list
	  * @return
	  */
	 private List<Map<String,Object>> getDataMapList(){
	  List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
	  Map<String,Object> map = null;
	  for(int i=0;i<3;i++){
	   map = new HashMap<String, Object>();
	   map.put("A", "1");
	   map.put("B", "2");
	   map.put("C", "3");
	   map.put("D", "4");
	   map.put("E", "5");
	   map.put("F", "6");
	   map.put("G", "7");
	   list.add(map);
	  }
	  return list;
	 }
	 
	 /**
	  * new Key
	  * @return
	  */
	 private Map<String,Object> getKeyMap(){
		  Map<String,Object> map = new HashMap<String, Object>();
		  map.put("A", "A1");
		  map.put("B", "B1");
		  map.put("C", "C1");
		  map.put("D", "D1");
		  map.put("E", "E1");
		  map.put("F", "F1");
		  map.put("G", "G1");
		  return map;
	 }
}
